<template>
	<el-main>
		<el-card class="box-card" shadow="never">
			<div class="search-box">
				<el-button-group>
					<el-button :type="buttonType(2)" @click="checkTab(2)">销售中({{ goodsNum.selling }})</el-button>
					<el-button :type="buttonType(3)" @click="checkTab(3)">已结束({{ goodsNum.store }})</el-button>
					<el-button :type="buttonType(4)" @click="checkTab(4)">待审核({{ goodsNum.check }})</el-button>
					<el-button :type="buttonType(5)" @click="checkTab(5)">未通过({{ goodsNum.refuse }})</el-button>
					<el-button :type="buttonType(6)" @click="checkTab(6)">已下架({{ goodsNum.offShelf }})</el-button>
				</el-button-group>
				<el-form :inline="true" :model="searchForm" class="demo-form-inline" style="margin-top: 20px">
					<el-form-item label="商品名称">
						<el-input v-model="searchForm.name" placeholder="" clearable></el-input>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" @click="onSubmit" icon="el-icon-search" >查询</el-button>
					</el-form-item>
				</el-form>
			</div>
			<div style="width:100%;height:0;border-bottom:#E4E7ED 1px dashed;margin-bottom: 10px"></div>
			<el-button type="primary" icon="el-icon-plus" @click="addGoods" style="margin-top: 10px">添加商品</el-button>
			<div style="width:100%;height:0;border-bottom:#E4E7ED 1px dashed;margin-top: 15px"></div>
			<el-table
				:data="tableData"
				style="width: 100%;">
				<el-table-column
					width="70"
					prop="id"
					label="ID">
				</el-table-column>
				<el-table-column
					width="80"
					label="商品图片">
					<template #default="scope">
						<img :src="scope.row.pic" style="width: 40px;height: 40px"/>
					</template>
				</el-table-column>
				<el-table-column
					prop="name"
					width="500"
					label="商品名">
				</el-table-column>
				<el-table-column
					prop="original_price"
					label="原价">
				</el-table-column>
				<el-table-column
					prop="seckill_price"
					label="秒杀价格">
				</el-table-column>
				<el-table-column
					prop="stock"
					label="秒杀库存">
				</el-table-column>
				<el-table-column
					width="200"
					label="秒杀时间">
					<template #default="scope">
						<p>{{ scope.row.start_time }}</p>
						<p>{{ scope.row.end_time }}</p>
					</template>
				</el-table-column>
				<el-table-column
					label="状态">
					<template #default="scope">
						<el-tag type="danger" v-if="scope.row.status == 1">未开启</el-tag>
						<el-tag type="success" v-if="scope.row.status == 2">进行中</el-tag>
						<el-tag type="info" v-if="scope.row.status == 3">已结束</el-tag>
					</template>
				</el-table-column>
				<el-table-column
					prop="operation"
					fixed="right"
					width="150px"
					label="操作">
					<template #default="scope">
						<el-button @click="handleUnshelf(scope.row)" type="text" size="small" v-if="searchForm.type == 2">下架</el-button>
						<el-button @click="handleShelf(scope.row)" type="text" size="small" v-if="searchForm.type == 6">上架</el-button>
						<el-button @click="handleEdit(scope.row)" type="text" size="small">编辑</el-button>
						<el-button @click="handleDel(scope.row)" type="text" size="small">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<Pages :form="searchForm" :page-obj="page" @pageChange="handlePageChange" @pageJump="getList"></Pages>

		</el-card>
	</el-main>

	<save-dialog v-if="dialogVisible" ref="saveDialog" @success="handleSuccess" @closed="dialogVisible=false" :close-on-click-modal="false"></save-dialog>
</template>

<script>
import saveDialog from './save'
import Pages from "@/components/pages/index.vue";

export default {
	components: {
		Pages,
		saveDialog
	},
	data() {
		return {
			searchForm: {
				type: "2",
				name: '',
				limit: 15,
				page: 1
			},
			page: {
				total: 0
			},
			dialogVisible: false,
			tableData: [],
			goodsNum: {
				selling: 0,
				store: 0,
				check: 0,
				refuse: 0,
				offShelf: 0
			},
		}
	},
	mounted() {
		this.getList()
	},
	methods: {
		// 获取列表
		async getList() {
			let res = await this.$API.seckill.list.get(this.searchForm)
			this.tableData = res.data.data
			this.page.total = res.data.total
			this.goodsNum = res.msg
		},
		// 添加秒杀
		addGoods() {
			this.dialogVisible = true

			this.$nextTick(() => {
				this.$refs.saveDialog.open('add')
			})
		},
		// 编辑秒杀
		handleEdit(row) {
			this.dialogVisible = true

			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
		// 搜索
		onSubmit() {
			this.searchForm.page = 1
			this.getList()
		},
		// 删除商品
		handleDel(item) {
			this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(async () => {
				let res = await this.$API.seckill.del.get({id: item.id})
				if (res.code == 0) {
					this.$message.success(res.msg)
					this.getList()
				} else {
					this.$message.error(res.msg)
				}
			}).catch(() => {});
		},
		// 分页
		handlePageChange(page) {
			this.searchForm.page = page
			this.getList()
		},
		handleSuccess() {
			this.dialogVisible = false
			this.getList()
		},
		// 选择样式
		buttonType(type) {
			if (this.searchForm.type == type) {
				return "primary";
			}
		},
		// 选择状态
		checkTab(type) {
			this.searchForm.type = type;
			this.getList();
		},
		// 上架活动
		handleShelf(row) {
			this.$confirm('此操作将上架该活动, 是否继续?', '提示', {
				confirmButtonText: '确定上架',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(async () => {
				let res = await this.$API.seckill.shelf.post({
					id: row.id,
					is_show: 1
				})
				if (res.code == 0) {
					this.$message.success(res.msg)
					this.getList()
				} else {
					this.$message.error(res.msg)
				}
			}).catch(() => {});
		},
		// 下架活动
		handleUnshelf(row) {
			this.$confirm('此操作将下架该活动, 是否继续?', '提示', {
				confirmButtonText: '确定下架',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(async () => {
				let res = await this.$API.seckill.shelf.post({
					id: row.id,
					is_show: 2
				})
				if (res.code == 0) {
					this.$message.success(res.msg)
					this.getList()
				} else {
					this.$message.error(res.msg)
				}
			}).catch(() => {});
		}
	}
}
</script>

<style scoped>
.el-button--small {
	padding: 0 !important;
}
</style>
